//
//  OosSiteQuery.m
//  com.oos.kraken
//
//  Created by Pablo Gonzalez on 2/16/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import <oos4ios/Options/OosReviewOptions.h>
#import "OosServiceQuery.h"
#import <oos4ios/ApiUrlProvider.h>

@implementation OosServiceQuery

static NSString *const NAME_KEY = @"name";
static NSString *const SLUG_KEY = @"siteSlug";
static NSString *const TITLE_KEY = @"title";
static NSString *const CONTENT_KEY = @"content";
static NSString *const PRIVACY_KEY = @"privacy";
static NSString *const TAGS_KEY = @"tags";
static NSString *const LISTS_KEY = @"lists";
static NSString *const ADDRESS_KEY = @"userAddress";
static NSString *const PHONE_KEY = @"phone";
static NSString *const LOCALITY_KEY = @"localityName";
static NSString *const COUNTRY_KEY = @"countryCode";
static NSString *const URL_KEY = @"url";
static NSString *const RATING_KEY = @"rating";
static NSString *const REDUCED_KEY = @"reduced";

@synthesize userSlug = mserSlug;
@synthesize serviceSlug = mServiceSlug;

- (void)buildQuery
{
    NSString *url = nil;
    ApiUrl *apiUrl = nil;
    if (self.userSlug) {
        apiUrl = [ApiUrlProvider provider].userServices;
        url = [apiUrl getUrl: self.userSlug, self.serviceSlug, nil];
    } else {
        apiUrl = [ApiUrlProvider provider].serviceDetails;
        if (self.serviceSlug) {
            url = [apiUrl getUrl: self.serviceSlug, nil];
        } else {
            [NSException raise:@"Invalid stae" 
                        format:@"Need to set a user slug before asking for a service slug"];
        }
    }
    if (url) {
        [self setFeedURL: [NSURL URLWithString:url]];
    }
}

+ (OosServiceQuery *)queryWithUserSlug:(NSString *)userSlug 
                       withServiceSlug:(NSString *)serviceSlug
{
    OosServiceQuery *query = [[[OosServiceQuery alloc] init] autorelease];
    query.userSlug = userSlug;
    query.serviceSlug = serviceSlug;
    [query buildQuery];
    return query;
}

+ (OosServiceQuery *)queryWithUserSlug:(NSString *)userSlug
{
    OosServiceQuery *query = [[[OosServiceQuery alloc] init] autorelease];
    query.userSlug = userSlug;
    [query buildQuery];
    return query;
}

+ (OosServiceQuery *)queryWithServiceSlug:(NSString *)serviceSlug
{
    OosServiceQuery *query = [[[OosServiceQuery alloc] init] autorelease];
    query.serviceSlug = serviceSlug;
    [query buildQuery];
    return query;
}

//- (void)addCustomParameterWithName:(NSString *)name value:(NSString *)value
//{
//    value = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    [super addCustomParameterWithName:name value:value];
//}

- (void)packStore:(OosOptionsStore *)store
{
    
    OosReviewOptions *options = store.reviewOptions;
    
    OosReview *review = options.review;
    OosService *service = options.service;
    if (!review && service) {
        review = service.myReview;
    }
    
    if (review) {
        [self addCustomParameterWithName:TITLE_KEY 
                                   value:review.title];
        [self addCustomParameterWithName:CONTENT_KEY 
                                   value:review.content];
        [self addCustomParameterWithName:PRIVACY_KEY 
                                   value:review.privacy.description];
        [self addCustomParameterWithName:TAGS_KEY 
                                   value:[[review.tags allObjects] componentsJoinedByString:@","]];
        [self addCustomParameterWithName:LISTS_KEY 
                                   value:[[review.lists allObjects] componentsJoinedByString:@","]];
        [self addCustomParameterWithName:RATING_KEY 
                                intValue:[review.rating intValue] 
                          removeForValue:0];
        if (review.service) {
            [self addCustomParameterWithName:SLUG_KEY 
                                       value:review.service.slug];
        }
    } else if (service) {
        
        [self addCustomParameterWithName:NAME_KEY 
                                   value:service.name];
        [self addCustomParameterWithName:ADDRESS_KEY 
                                   value:service.location.address];
        [self addCustomParameterWithName:PHONE_KEY 
                                   value:service.telephone];
        [self addCustomParameterWithName:LOCALITY_KEY 
                                   value:service.location.locality.value];
        NSString *country = service.location.country.value;
        if (!country) {
            country = @"es";
        }
        [self addCustomParameterWithName:COUNTRY_KEY 
                                   value:country];
        [self addCustomParameterWithName:URL_KEY 
                                   value:service.urlString];
    }
    
    [self addCustomParameterWithName:REDUCED_KEY  value:@"true"];
    
    [super packStore:store];
    
}

- (void)dealloc
{
    self.userSlug = nil;
    self.serviceSlug = nil;
    [super dealloc];
}

@end
